﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;

namespace Cryptography_0912089_0912100
{
    public partial class HashForm : Form
    {
        //Bài 4 mã hóa, hash function: 
        //  Chọn một file, chọn một thuật toán hash phát sinh file hash
        //  Chọn file hash, file chưa hash compare có giống nhau không

        //Bài 5 mã hóa, chữ ký điện tử: phát sinh ra khóa, tạo chữ ký , verify chữ ký.

        private HashHelper hashHelper;

        public HashForm()
        {
            InitializeComponent();
        }

        private void buttonBrowseGen_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogFileInputGen.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFileInputGen.Text = openFileDialogFileInputGen.FileName;
                textBoxHashFileOutputGen.Text = textBoxFileInputGen.Text + ".hash";
            }
        }

        private void buttonBrowseFileInputCom_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogFileInputCom.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxFileInputCom.Text = openFileDialogFileInputCom.FileName;
            }
        }

        private void buttonHashFileInputCom_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialogHashFileInputCom.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                textBoxHashFileInputCom.Text = openFileDialogHashFileInputCom.FileName;

                textBoxChecksumCom.Text = ReadFile(textBoxHashFileInputCom.Text);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<string> listHashAlgorithm = new List<string>();
            listHashAlgorithm.Add("MD5");
            listHashAlgorithm.Add("RIPEMD160");
            listHashAlgorithm.Add("SHA1");
            listHashAlgorithm.Add("SHA256");
            listHashAlgorithm.Add("SHA384");
            listHashAlgorithm.Add("SHA512");
            listHashAlgorithm.Add("KeyedHashAlgorithm");

            foreach (string str in listHashAlgorithm)
            {
                comboBoxHashAlgorithmCom.Items.Add(str);
                comboBoxHashAlgorithmGen.Items.Add(str);
            }

            comboBoxHashAlgorithmCom.SelectedIndex = 0;
            comboBoxHashAlgorithmGen.SelectedIndex = 0;
        }

        private void buttonHash_Click(object sender, EventArgs e)
        {
            if (textBoxFileInputGen.Text == "")
            {
                MessageBox.Show("You must choose a FILE to get hash string", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (comboBoxHashAlgorithmGen.SelectedIndex)
            {
                case 0:
                    hashHelper = new MD5Hasher();
                    break;
                case 1:
                    hashHelper = new RIPEMD160Hasher();
                    break;
                case 2:
                    hashHelper = new SHA1Hasher();
                    break;
                case 3:
                    hashHelper = new SHA256Hasher();
                    break;
                case 4: 
                    hashHelper = new SHA384Hasher();
                    break;
                case 5:
                    hashHelper = new SHA512Hasher();
                    break;
                case 6:
                    hashHelper = new KeyedHashAlgorithmHasher();
                    break;
            }

            string hash = hashHelper.HashFile(textBoxFileInputGen.Text);
            textBoxChecksumGen.Text = hash;

            WriteFile(hash, textBoxHashFileOutputGen.Text);
        }

        private void WriteFile(string content, string outputFileName)
        {
            StreamWriter streamWriter = new StreamWriter(outputFileName);
            streamWriter.Write(content);
            streamWriter.Flush();
            streamWriter.Close();
        }

        private string ReadFile(string inputFileName)
        {
            StreamReader streamReader = new StreamReader(inputFileName);
            string result = streamReader.ReadToEnd();
            return result;
        }

        private void buttonCompare_Click(object sender, EventArgs e)
        {
            if (textBoxFileInputCom.Text == "")
            {
                MessageBox.Show("You must choose a FILE to compare", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (textBoxHashFileInputCom.Text == "")
            {
                MessageBox.Show("You must choose a HASH FILE to compare", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (comboBoxHashAlgorithmCom.SelectedIndex)
            {
                case 0:
                    hashHelper = new MD5Hasher();
                    break;
                case 1:
                    hashHelper = new RIPEMD160Hasher();
                    break;
                case 2:
                    hashHelper = new SHA1Hasher();
                    break;
                case 3:
                    hashHelper = new SHA256Hasher();
                    break;
                case 4:
                    hashHelper = new SHA384Hasher();
                    break;
                case 5:
                    hashHelper = new SHA512Hasher();
                    break;
                case 6:
                    hashHelper = new KeyedHashAlgorithmHasher();
                    break;
            }

            string hash = hashHelper.HashFile(textBoxFileInputCom.Text);
            
            if(hash == textBoxChecksumCom.Text)
                MessageBox.Show("File input has the SAME hash value", "Same hash value");
            else
                MessageBox.Show("File input has DIFFERENT hash value", "Different hash value", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }
    }
}
